/**
 * Eclipse Outliner
 * Copyright (C) 2006 Jeff Mesnil
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.jmesnil.outliner.ui.internal;

import java.io.InputStream;

import net.jmesnil.opml.core.IOPML;
import net.jmesnil.opml.core.OPMLPlugin;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

public class OutlinerEditorInput implements IEditorInput {

    private IOPML opml;

    private IFileStore fileStore;

    public OutlinerEditorInput(IFileStore fileStore) {
        this.fileStore = fileStore;
        try {
            InputStream stream = fileStore.openInputStream(EFS.NONE, null);
            StringBuffer out = new StringBuffer();
            byte[] b = new byte[4096];
            for (int n; (n = stream.read(b)) != -1;) {
                out.append(new String(b, 0, n));
            }
            opml = OPMLPlugin.getDefault().fromXml(out.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public OutlinerEditorInput(IOPML opml) {
        this.opml = opml;
    }

    public boolean exists() {
        return false;
    }

    public ImageDescriptor getImageDescriptor() {
        return null;
    }

    public String getName() {
        return opml.getHead().getTitle();
    }

    public IPersistableElement getPersistable() {
        return null;
    }

    public String getToolTipText() {
        return opml.getHead().getTitle();
    }

    public Object getAdapter(Class adapter) {
        return null;
    }

    public IOPML getOPML() {
        return opml;
    }

    public IFileStore getFileStore() {
        return fileStore;
    }
    
    public void setFileStore(IFileStore fileStore) {
        Assert.isNotNull(fileStore);
        this.fileStore = fileStore;
    }

    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result
                + ((fileStore == null) ? 0 : fileStore.hashCode());
        result = PRIME * result + ((opml == null) ? 0 : opml.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final OutlinerEditorInput other = (OutlinerEditorInput) obj;
        if (fileStore == null) {
            if (other.fileStore != null)
                return false;
        } else if (other.fileStore != null) {
            if (fileStore.toURI().equals(other.fileStore.toURI())) {
                return true;
            }
        }
        if (opml == null) {
            if (other.opml != null)
                return false;
        } else if (!opml.equals(other.opml))
            return false;
        return true;
    }
}
